package com.crm.dao.impl;

import java.util.List;
import java.util.Map;

import com.crm.dao.LinkmanDao;
import com.crm.pojo.Linkman;
import com.crm.pojo.PageInfo;
import com.crm.util.JdbcUtil;

public class LinkmanDaoImpl implements LinkmanDao {

	@Override
	public int insertLinkman(Linkman linkman) {
		String sql = "insert into cst_linkman(lkm_name,lkm_cust_id,lkm_gender,lkm_phone,lkm_mobile,lkm_email,lkm_qq,lkm_position,lkm_memo) values(?,?,?,?,?,?,?,?,?)";
		String lkmName = linkman.getLkmName();
		Integer lkmCustId = linkman.getLkmCustId();
		String lkmGender = linkman.getLkmGender();
		String lkmPhone = linkman.getLkmPhone();
		String lkmMobile = linkman.getLkmMobile();
		String lkmEmail = linkman.getLkmEmail();
		String lkmQQ = linkman.getLkmQQ();
		String lkmPosition = linkman.getLkmPosition();
		String lkmMemo = linkman.getLkmMemo();
		int n = JdbcUtil.executeUpdate(sql, lkmName, lkmCustId, lkmGender, lkmPhone, lkmMobile, lkmEmail, lkmQQ, lkmPosition, lkmMemo);
		return n;
	}

	@Override
	public PageInfo queryLinkman(String lkmName, Integer pageSize, Integer pageNumber) {
		PageInfo page = new PageInfo();
		page.setPageSize(pageSize);
		page.setPageNumber(pageNumber);
		String sql = "select * from cst_linkman l left join cst_customer c on l.lkm_cust_id=c.cust_id where 1=1";
		if(lkmName!=null && !"".equals(lkmName)) {
			sql += " and lkm_name like '%"+lkmName+"%'";
		}
		page.setTotalCount(getTotalCount(sql));
		sql += " limit "+(pageNumber-1)*pageSize+","+pageSize;
		List<Map> linkmans = JdbcUtil.executeQuery(sql, null);
		page.setResult(linkmans);
		return page;
	}
	private Integer getTotalCount(String sql) {
		String newSql = "select count(*) from ("+sql+") c";
		List<Map> linkmans = JdbcUtil.executeQuery(newSql, null);
		String linkman = "";
		for(Map l:linkmans) {
			linkman = l.get("count(*)").toString();
		}
		return Integer.parseInt(linkman);
	}

	@Override
	public int deleteLinkman(Integer lkmId) {
		String sql = "delete from cst_linkman where lkm_id=?";
		return JdbcUtil.executeUpdate(sql, lkmId);
	}

	@Override
	public List<Map> queryLinkman(Integer lkmId) {
		String sql = "select * from cst_linkman l left join cst_customer c on l.lkm_cust_id=c.cust_id where lkm_id=?";
		return JdbcUtil.executeQuery(sql, lkmId);
	}

	@Override
	public int updateLinkman(Linkman linkman) {
		String sql = "update  cst_linkman set lkm_name=?,lkm_cust_id=?,lkm_gender=?,lkm_phone=?,lkm_mobile=?,lkm_email=?,lkm_qq=?,lkm_position=?,lkm_memo=? where lkm_id=?";
		String lkmName = linkman.getLkmName();
		Integer lkmCustId = linkman.getLkmCustId();
		String lkmGender = linkman.getLkmGender();
		String lkmPhone = linkman.getLkmPhone();
		String lkmMobile = linkman.getLkmMobile();
		String lkmEmail = linkman.getLkmEmail();
		String lkmQQ = linkman.getLkmQQ();
		String lkmPosition = linkman.getLkmPosition();
		String lkmMemo = linkman.getLkmMemo();
		Integer lkmId = linkman.getLkmId();
		int n = JdbcUtil.executeUpdate(sql, lkmName, lkmCustId, lkmGender, lkmPhone, lkmMobile, lkmEmail, lkmQQ, lkmPosition, lkmMemo, lkmId);
		return n;
	}

	@Override
	public List<Map> queryLkm(Integer custId) {
		String sql = "select * from cst_linkman where lkm_cust_id=?";
		return JdbcUtil.executeQuery(sql, custId);
	}
}
